using UnityEngine;

public class InputControl : MonoBehaviour
{
	public struct Touch
	{
		public Vector3 pos;

		public int id;

		public bool isDown;

		public bool isUp;
	}

	public static BetterList<Touch> touches = new BetterList<Touch>();

	public static BetterList<Touch> trackedTouches = new BetterList<Touch>();

	public static bool buttonDown { get; protected set; }

	public static bool buttonUp { get; protected set; }

	public static bool mouseButton { get; protected set; }

	public static Touch firstDownTouch
	{
		get
		{
			BetterList<Touch> betterList = trackedTouches;
			for (int i = 0; i < betterList.Count; i++)
			{
				Touch result = betterList[i];
				if (result.isDown && !result.isUp)
				{
					return result;
				}
			}
			Touch result2 = default(Touch);
			result2.id = -1;
			return result2;
		}
	}

	public static bool isTrackedTouchDown
	{
		get
		{
			BetterList<Touch> betterList = trackedTouches;
			for (int i = 0; i < betterList.Count; i++)
			{
				Touch touch = betterList[i];
				if (touch.isDown && !touch.isUp)
				{
					return true;
				}
			}
			return false;
		}
	}

	public static Touch GetTouch(int id)
	{
		int touchIndex = GetTouchIndex(id, trackedTouches);
		if (touchIndex >= 0)
		{
			return trackedTouches[touchIndex];
		}
		Touch result = default(Touch);
		result.id = -1;
		result.isUp = true;
		return result;
	}

	public static void ClearTrackedTouches()
	{
		trackedTouches.Clear();
	}

	private void Awake()
	{
		buttonDown = false;
		buttonUp = false;
	}

	private void OnPress(bool pressed)
	{
		if (pressed)
		{
			buttonDown = true;
			mouseButton = true;
		}
		Touch touch = default(Touch);
		touch.isDown = pressed;
		touch.isUp = !pressed;
		touch.id = UICamera.currentTouchID;
		touch.pos = UICamera.currentTouch.pos;
		touches.Add(touch);
		int num = -1;
		num = ((!pressed) ? GetTouchIndex(UICamera.currentTouchID, trackedTouches) : GetOrCreateTouchIndex(UICamera.currentTouchID, trackedTouches));
		if (num >= 0)
		{
			Touch touch2 = trackedTouches[num];
			touch2 = touch;
			trackedTouches[num] = touch2;
		}
	}

	protected static int GetTouchIndex(int touchId, BetterList<Touch> touches)
	{
		for (int i = 0; i < touches.Count; i++)
		{
			if (touches[i].id == touchId)
			{
				return i;
			}
		}
		return -1;
	}

	protected static int GetOrCreateTouchIndex(int touchId, BetterList<Touch> touches)
	{
		int touchIndex = GetTouchIndex(touchId, touches);
		if (touchIndex >= 0)
		{
			return touchIndex;
		}
		Touch item = default(Touch);
		item.id = touchId;
		touches.Add(item);
		return touches.Count - 1;
	}

	private void OnDrag(Vector2 delta)
	{
		int orCreateTouchIndex = GetOrCreateTouchIndex(UICamera.currentTouchID, touches);
		Touch value = touches[orCreateTouchIndex];
		value.id = UICamera.currentTouchID;
		value.pos = UICamera.currentTouch.pos;
		touches[orCreateTouchIndex] = value;
		orCreateTouchIndex = GetTouchIndex(UICamera.currentTouchID, trackedTouches);
		if (orCreateTouchIndex >= 0)
		{
			value = trackedTouches[orCreateTouchIndex];
			value.pos = UICamera.currentTouch.pos;
			trackedTouches[orCreateTouchIndex] = value;
		}
	}

	private void Update()
	{
		if (Input.GetMouseButtonUp(0))
		{
			buttonUp = true;
			mouseButton = false;
		}
		for (int i = 0; i < Input.touchCount; i++)
		{
			UnityEngine.Touch touch = Input.GetTouch(i);
			int touchIndex = GetTouchIndex(touch.fingerId, trackedTouches);
			if (touchIndex >= 0)
			{
				Touch value = trackedTouches[touchIndex];
				if (touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended)
				{
					value.isUp = true;
				}
				value.pos = touch.position;
				trackedTouches[touchIndex] = value;
			}
		}
		BetterList<Touch> betterList = trackedTouches;
		for (int j = 0; j < betterList.Count; j++)
		{
			Touch touch2 = betterList[j];
		}
	}

	private void LateUpdate()
	{
		buttonUp = false;
		buttonDown = false;
		touches.Clear();
		BetterList<Touch> betterList = trackedTouches;
		for (int i = 0; i < betterList.Count; i++)
		{
			Touch value = betterList[i];
			if (value.isUp)
			{
				betterList.RemoveAt(i);
				i--;
			}
			else
			{
				value.isUp = (value.isDown = false);
				betterList[i] = value;
			}
		}
	}
}
